/**
 * 
 */
package br.com.pim.action;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.com.pim.connection.ConnectionFactory;
import br.com.pim.dao.EstoqueDAO;
import br.com.pim.dao.ProdutoDAO;
import br.com.pim.form.EstoqueForm;
import br.com.pim.model.EstoqueModel;

/**
 * @author douglas.peixoto
 *
 */
public class EstoqueAction extends DispatchAction {
	
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		
		Connection myConn = null;
		EstoqueDAO eDAO = null;
		ProdutoDAO pDAO = null;
			try{
				myConn = new ConnectionFactory().getConnectionMySQL();
				eDAO = new EstoqueDAO(myConn);
				pDAO= new ProdutoDAO(myConn);
				req.setAttribute("LISTA_PRODUTOS", pDAO.buscarTodos());
				req.setAttribute("LISTA_ESTOQUE", eDAO.buscarEstoque());
			}finally{
				if(!(myConn == null)){
					myConn.close();
				}
					
			}
		
		return mapping.getInputForward();
	}

	
	public ActionForward atualizaEstoque(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
				
			Connection myConn = null;
			EstoqueForm eForm = (EstoqueForm) form;
			EstoqueModel eModel = null;
			EstoqueDAO eDAO = null;
			
			
				try{
					myConn = new ConnectionFactory().getConnectionMySQL();
					eDAO = new EstoqueDAO(myConn);
					eModel = eForm.getEstoque();
					eDAO.atualizarEstoque(eForm.getQuantidade(), eModel.getCdProduto());
				}finally{
					if(!(myConn == null)){
						myConn.close();
					}
					this.iniciar(mapping, eForm, req, resp);
				}
				return mapping.getInputForward();
	}
}
